package hehe.json.json;

import com.jsoniter.JsonIterator;
import com.jsoniter.output.JsonStream;
import hehe.json.BaseJson;
import hehe.json.util.JsonUtil;

/**
 * @todo 这里写类名
 * <B>说明：</B>
 * <pre>
 *  略
 * </pre>
 * <B>示例：</B>
 * <pre>
 *  略
 * </pre>
 * <B>日志：</B>
 * <pre>
 *  略
 * </pre>
 * <B>注意事项：</B>
 * <pre>
 *  略
 * </pre>
 */
public class Jsoniter extends BaseJson
{
    protected String jsonStreamClazz = "com.jsoniter.output.JsonStream";

    protected String jsonIteratorClazz = "com.jsoniter.JsonIterator";

    protected Class jsonStream = null;
    protected Class jsonIterator = null;

    @Override
    public String format(Object value) throws Exception
    {
        return (String) JsonUtil.invoke(this.getJsonStream(),"serialize",new Object[]{value},new Class[]{Object.class});

    }

    @Override
    public <T> T parser(String json, Class<T> type) throws Exception
    {
        return (T)JsonUtil.invoke(this.getJsonIterator(),"deserialize",
                new Object[]{json,type},new Class[]{String.class,Class.class});
    }

    public Class getJsonStream()
    {
        if (this.jsonStream == null) {
            this.jsonStream = JsonUtil.getClazz(this.jsonStreamClazz);
        }

        return this.jsonStream;
    }

    public Class getJsonIterator()
    {
        if (this.jsonIterator == null) {
            this.jsonIterator = JsonUtil.getClazz(this.jsonIteratorClazz);
        }

        return this.jsonIterator;
    }
}
